 <template>
  <div class="container">
    <el-drawer
      title="留言板"
      :visible.sync="drawerVisible.drawer2"
      direction="rtl"
      size="350px"
      :modal="false"
      :show-close="false"
      custom-class="draw-comments"
      @close="closeDrawer('drawer2')"
    >
      <div class="buy-img">
        <div class="img-word"><span>上传图片</span></div>
        <!-- 图像预览 -->
        <div class="pre_img">
          <!-- 图片，用来展示用户选择的头像 -->
          <img
            v-if="!pre_img"
            class="the_img"
            src="../../../assets/defautl.svg"
            alt=""
          />
          <!-- 这里preavatar是没有压缩的 -->
          <img
            v-else
            class="the_img"
            alt=""
            :src="pre_img"
          >
        </div>

        <!-- 按钮区域 -->
        <div class="btn-box">
          <input
            type="file"
            ref="iptRef"
            accept="image/*"
            style="display: none"
            @change="onFileChange"
          />
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="chooseImg"
          >选择图片</el-button>

        </div>
      </div>
      <div class="buy-dir">
        <div class="img-word"><span>留言</span></div>
        <div class="userWords">
          <textarea
            class="message"
            placeholder="请输入..."
            v-model="formdata.message"
          ></textarea>
        </div>
      </div>

      <el-button
        type="success"
        icon="el-icon-upload"
        @click="addWall"
      >提交</el-button>
    </el-drawer>
    <NoteCard
      v-for="(item,index) in wallCards"
      :key="index"
      :card="item"
    ></NoteCard>

    <button
      class="edit-button"
      @click="openDrawer('drawer2')"
    >
      <span class="edit-button-span">📝</span>
    </button>

  </div>

</template>
  <script>
import NoteCard from "./noteCard";
import { mapState } from "vuex";
import { addWallAPI } from "@/api";
export default {
  data() {
    return {
      pre_img: "",

      drawerVisible: {
        drawer1: false,
        drawer2: false,
      },
      filename: "",
      file: "",
      formdata: {
        message: "",
        img_url: "",
      },
    };
  },
  components: {
    NoteCard,
  },
  methods: {
    openDrawer(drawerName) {
      console.log(drawerName);
      if (drawerName == "drawer1") {
        this.drawerVisible.drawer1 = true;
      } else {
        this.drawerVisible.drawer2 = true;
      }
    },
    closeDrawer(drawername) {
      if (drawername == "drawer1") {
        this.drawerVisible.drawer1 = false;
      } else {
        this.drawerVisible.drawer2 = false;
      }
    },

    // 选择图片
    chooseImg() {
      this.$refs.iptRef.click();
    },
    onFileChange(e) {
      let files = e.target.files;
      if (files.length == 0) {
        // 证明没有选
      } else {
        this.file = e.target.files[0];
        this.filename = this.file.name;
        const reader = new FileReader();
        reader.readAsDataURL(this.file);
        reader.onload = (e) => {
          this.compressImage2(e.target.result); //处理图片
          this.pre_img = e.target.result; //未压缩
        };
      }
    },

    // 压缩图片
    compressImage2: function (base64data) {
      var _this = this;
      var maxWidth = 800; // 设置最大宽度
      var maxHeight = 600; // 设置最大高度
      var quality = 0.75; // 设置压缩质量

      var img = new Image();

      img.onload = function () {
        var width = img.width;
        var height = img.height;

        // 计算压缩后的尺寸
        if (width > height) {
          if (width > maxWidth) {
            height *= maxWidth / width;
            width = maxWidth;
          }
        } else {
          if (height > maxHeight) {
            width *= maxHeight / height;
            height = maxHeight;
          }
        }

        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        canvas.width = width;
        canvas.height = height;
        ctx.drawImage(img, 0, 0, width, height);

        // 将 Canvas 数据转换为 Base64 字符串
        var compressedData = canvas.toDataURL("image/jpeg", quality);

        _this.formdata.img_url = compressedData;
      };

      img.src = base64data;
    },

    // 提交发布留言
    async addWall() {
      // 获取用户信息
      console.log("word word", this.wallCards);
      await this.$store.dispatch("initUserInfo");

      const res = await addWallAPI(this.formdata);
      if (res.status == 0) this.$message.success("发布成功");

      console.log("测试测试测试", this.wallCards);
      this.$store.dispatch("getUserLikeWallID");
      this.$store.dispatch("initWallCards");
      this.formdata.message = "";
      this.formdata.img_url = "";
      this.pre_img = "";
      setTimeout(() => {
        this.closeDrawer("drawer2");
      }, 500);
    },
  },
  computed: {
    src() {
      return;
    },
    ...mapState({
      wallCards: (state) => state.walls.wallCards,
    }),
  },
  watch: {},
  mounted() {
    this.$store.dispatch("initWallCards");
    this.$store.dispatch("initUserInfo");
    console.log(this.src);
  },
};
</script>

<style scoped>
.container {
  display: flex;
  position: relative;
  flex-wrap: wrap; /* 当超出容器宽度时自动换行 */
  gap: 20px; /* 设置子元素之间的间距 */
  height: 100%;
  justify-content: center;
  align-items: flex-start;
}
/* 发表编辑留言 */
/* 提交图片滚动条样式 */
/*定义滚动条宽度和颜色*/
.pre_img::-webkit-scrollbar {
  width: 4px;
}

.pre_img::-webkit-scrollbar-track {
  background: #d2f3ef;
}

.pre_img::-webkit-scrollbar-thumb {
  background: #94f0f0;
}

/* 悬停效果 */
.pre_img::-webkit-scrollbar-thumb:hover {
  background: #5cd0f4;
}
/*提交按钮 */
.el-button--success {
  margin-bottom: 20px;
}

/* 商品上传用户头像 */

/* 商品上传头像信息 */

.img-word {
  width: 100%;
  display: flex;
  justify-content: start;
  align-items: center;
  margin-bottom: 10px;
  opacity: 0.8;
}

.buy-img {
  display: flex;
  width: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.pre_img {
  width: 250px;
  height: 250px;
  overflow-y: auto;
  border: 1px solid;
  margin-bottom: 15px;
}
.pre_img > img {
  width: 100%;
  height: auto;
}
/* 结束 */
/* 物品说明介绍 */
.buy-dir {
  width: 100%;
  height: 40%;
  display: flex;

  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  margin-top: -10px;
  opacity: 0.6;
}
.userWords {
  display: flex;
  background-color: #acdccec3;
  margin-top: 4px;
  width: 100%;
  height: 40%;
  padding: 10px;
  justify-content: center;
  align-items: center;
  position: relative;
  box-sizing: border-box;
  max-width: 300px;
  font-family: cursive;
  font-size: 20px;
  border-radius: 10px;
  background: #fff;
  background-image: linear-gradient(#f5f5f0 1.1rem, #ccc 1.2rem);
  background-size: 100% 1.2rem;
  line-height: 1.45rem;
  padding: 0.5rem 0.2rem 0.1rem 0.1rem;
}
/* textarea */
.message {
  background: none;
  /* border: none; */
  border: none;
  resize: none;
  outline: none;
  box-sizing: border-box;
  padding: 8px;
  width: 100%;
  height: 100%;
  margin-top: 12px;
}

.userWords > span {
  width: 96%;
  height: 90%;
  overflow: auto; /* 溢出隐藏 */
  text-align: center;
}
/*定义滚动条宽度和颜色*/
.userWords-span::-webkit-scrollbar {
  width: 4px;
}

.userWords-span::-webkit-scrollbar-track {
  background: #d2f3ef;
}

.userWords-span::-webkit-scrollbar-thumb {
  background: #94f0f0;
}

/* 悬停效果 */
.userWords-span::-webkit-scrollbar-thumb:hover {
  background: #5cd0f4;
}
/* 抽屉评论 */
::v-deep .el-drawer__body {
  width: 95%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
::v-deep .el-drawer.rtl {
  height: 100%;
  background-color: #b6dbfe;
  border-radius: 10px;
  right: 2%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
::v-deep .el-drawer__container {
  position: relative;

  display: flex;

  left: 0;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  bottom: 0;
  height: 90%;
  width: 100%;
}

::v-deep .el-drawer__wrap.draw-comments {
  background-color: #98c5e5;
}

/* 结束 */
/* 编辑按钮 */
.edit-button {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #e8e8e8;
  background-color: #959ff8;
  width: 60px;
  height: 60px;
  font-size: 24px;
  text-transform: uppercase;
  border: 2px solid #5bd6fcbb;
  border-radius: 50%;
  transition: all 0.6s cubic-bezier(0.23, 1, 0.32, 1);
  box-shadow: 5px 5px 2px rgba(0, 0, 0, 0.15), 2px 2px 2px rgba(0, 0, 0, 0.1),
    -3px -3px 2px rgba(255, 255, 255, 0.05),
    -2px -2px 1px rgba(255, 255, 255, 0.05);
  overflow: hidden;
  cursor: pointer;
  z-index: 7;
  right: 35px;
  top: 15%;
}
.edit-button-span {
  position: relative;
  z-index: 2;
  transition: all 0.6s cubic-bezier(0.23, 1, 0.32, 1);
}
/* 编辑按钮结束 */
</style>